package com.mypaper.list;

import com.mypaper.dag.DAG;
import com.mypaper.elem.IntElement;


public class AdjoinTable{
	/*该类根据已知的DAG图，求每个节点的孩子节点及父节点
	 * 用邻接表表示
	 */
	public int vertexNum;
	public UnorderLinkedList[] vertexChild;
	public UnorderLinkedList[] vertexPare;
	public AdjoinTable(){
		vertexNum=0;
		vertexChild=new UnorderLinkedList[vertexNum];
		vertexPare=new UnorderLinkedList[vertexNum];
	}
	
	public AdjoinTable(DAG dag){
		this.vertexNum=dag.vertexNum;
		this.vertexChild=getChild(dag);
		this.vertexPare=getPare(dag);
	}
	
	public UnorderLinkedList[] getChild(DAG dag){
		UnorderLinkedList[] children=new UnorderLinkedList[vertexNum];
		IntElement newElement=new IntElement();
		for(int i=0; i<this.vertexNum; i++){
			children[i]=new UnorderLinkedList();
			for(int j=0; j<this.vertexNum; j++){
				if(dag.edgeWeight[i][j]!=0&&dag.edgeWeight[i][j]!=Float.MAX_VALUE){
					newElement.setNum(j);
					children[i].insertNode(newElement);
				}
			}
		}
		return children;
	}
	
	public UnorderLinkedList[] getPare(DAG dag){
		UnorderLinkedList[] parent=new UnorderLinkedList[vertexNum];
		IntElement newElement=new IntElement();
		for(int i=0; i<this.vertexNum; i++){
			parent[i]=new UnorderLinkedList();
			for(int j=0; j<this.vertexNum; j++){
				if(dag.edgeWeight[j][i]!=0&&dag.edgeWeight[j][i]!=Float.MAX_VALUE){
					newElement.setNum(j);
					parent[i].insertNode(newElement);
				}
			}
		}
		return parent;
	}
	public void print(){
		System.out.println("The children of each node");
		for(int i=0; i<vertexNum; i++){
			System.out.print("Vertex["+i+"]:");
			vertexChild[i].print();
			System.out.println();
		}
		System.out.println();
		System.out.println("The parent of each node");
		for(int i=0; i<vertexNum; i++){
			System.out.print("Vertex["+i+"]:");
			vertexPare[i].print();
			System.out.println();
		}
	}
	
}
